|
|
|
|
|
|
|
Figure 17.11
You can use code to override the default
size and position of an MDI child form. |
|
|
|
|
|
|
|
|
Creating Forms Using Object Variables |
|
|
|
|
|
|
|
|
After you create the basic child form, you need a means to create an instance of the form (at runtime) and display it in the MDI application. Doing so requires all of two code lines. First, you use a Dim statement to create an object variable (a variable of Object type) that will contain an instance of the form. In the Dim statement, you need to use the New keyword to tell Visual Basic to create a new instance of the form. Otherwise, the statement just creates a new handle to the existing form. After you create the object variable, you use the Show method to display the form. However, instead of using the form name, you specify the name of the variable. The two required lines of code are shown here: |
|
|
|
|
|
|
|
|
Dim NewFrm As New frmText
NewFrm.Show |
|
|
|
|
|
|
|
|
To see how this code works, place the lines of code in the Click event procedure of the MDI form, and then run the program. Each time you click the MDI form, a new instance of the child form is displayed. |
|
|
|
|
|
|
|
|
Using the Keywords Me and ActiveForm |
|
|
|
|
|
|
|
|
Because all the child forms are the same, and you use the same variable to create each of them, how can you know which form to specify when running codeespecially code that is generic and can work with any of the forms? |
|
|
|
|
|
|
|
|
You will use two particular keywords extensively in working with MDI applications: Me and ActiveForm. These two keywords let you create generic code routines that work with any child form that you create. |
|
|
|
|
|
|
|
|
Me is a keyword that can be used in any form to refer to itself, just as you can use the word me to refer to yourself without having to use your name. You saw how this keyword was used in Listing 17.1 to size and position the child form on startup. If you write all code in the child form using Me to refer to the form name, your code will work for whichever instance of the form is active at the time. |
|
|
|
|
|